package com.bsoft.exchange.service;

import com.bsoft.exchange.dao.his.OutpatientMapper;
import com.bsoft.exchange.dao.his.SystemConfigMapper;
import com.bsoft.exchange.dao.portal.HospitalInformationMapper;
import com.bsoft.exchange.domain.*;
import com.bsoft.exchange.pojo.*;
import com.bsoft.exchange.util.BaseResponse;
import com.bsoft.exchange.util.Constant;
import com.bsoft.exchange.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OutpatientService {
	
	@Autowired
	OutpatientMapper outpatientMapper;
	
	@Autowired
	HospitalInformationMapper hospitalInformationMapper;
	
	@Autowired
	SystemConfigMapper systemConfigMapper;
	
	/**
	 * 获取门诊费用列表
	 * @return
	 */
	public BaseResponse<List<ListOutpatientExpenses>> listOutpatientExpenses(ListOutpatientExpensesPojo listOutpatientExpensesPojo){
		
		BaseResponse<List<ListOutpatientExpenses>> entry = new BaseResponse<>();
		entry.setCode(Constant.successTag);
		entry.setMessage("处理成功");
		
		List<ListOutpatientExpenses> list = new ArrayList<>();
		entry.setData(list);;
		
		return entry;
	}
	
	/**
	 * 获取门诊费用详情
	 * @return
	 */
	public OutpatientExpensesDetai outpatientExpensesDetai(OutpatientExpensesDetaiPojo outpatientExpensesDetaiPojo) {
		
		OutpatientExpensesDetai  outpatientExpensesDetai = new OutpatientExpensesDetai(); 
		
		return outpatientExpensesDetai;
	}
	
	/**
	 * 获取处置记录。
	 * @return
	 */
	public BaseResponse<List<ListDisposalrecordDomain>> listDisposalrecord(AdmNumberPojo admNumberPojo){
		
		BaseResponse<List<ListDisposalrecordDomain>> response = new BaseResponse<>();
		response.setCode(Constant.successResponse);
		response.setData(null);
		try {
			
			 Long jgid;
			 Long jzxh;
	         if(admNumberPojo.getOrganizationId() == null || "".equals(admNumberPojo.getOrganizationId())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("医院识别代码不能为空!");
	        	  
	             return response;
	         }
	           
	        jgid = Long.parseLong(admNumberPojo.getOrganizationId());
			
	        if(admNumberPojo.getAdmNumber() == null || "".equals(admNumberPojo.getAdmNumber())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("就诊序号不能为空!");
	        	  
	             return response;
	         }
	           
	        jzxh = Long.parseLong(admNumberPojo.getAdmNumber());
	        
	        List<ListDisposalrecordDomain> czList = outpatientMapper.listDisposalrecord(jgid, jzxh);
	        if(czList == null ||czList.size() < 1){
	        	response.setMessage("未查询到数据！");
	        }else{
	        	response.setMessage("处理成功");
				response.setData(czList);
	        }
			
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(Constant.errorTag);
			response.setMessage("处理失败"+e.getMessage());
		}
		
		
		return response;
	}
	
	/**
	 * 获取历史就诊记录。
	 * @return
	 */
	public BaseResponse<List<ListHistoryRecordDomain>> listHistoryRecord(ListHistoryRecordPojo listHistoryRecordPojo){
		
		BaseResponse<List<ListHistoryRecordDomain>> response = new BaseResponse<>();
		response.setCode(Constant.successResponse);
		response.setData(null);
		try {
			
			 Long jgid;
			 Long brid;
	         if(listHistoryRecordPojo.getOrganizationId() == null || "".equals(listHistoryRecordPojo.getOrganizationId())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("医院识别代码不能为空!");
	        	  
	             return response;
	         }
	           
	        jgid = Long.parseLong(listHistoryRecordPojo.getOrganizationId());
			
	        if(listHistoryRecordPojo.getPatientId() == null || "".equals(listHistoryRecordPojo.getPatientId())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("病人id不能为空!");
	        	  
	             return response;
	         }
	           
	        brid = Long.parseLong(listHistoryRecordPojo.getPatientId());

			if(listHistoryRecordPojo.getPageNo() == null || "".equals(listHistoryRecordPojo.getPageNo())){
				response.setCode(Constant.errorTag);
				response.setMessage("入参页码不能为空!");

				return response;
			}

			if(listHistoryRecordPojo.getPageSize() == null || "".equals(listHistoryRecordPojo.getPageSize())){
				response.setCode(Constant.errorTag);
				response.setMessage("入参每页条数不能为空!");

				return response;
			}


	        List<ListHistoryRecordDomain> jzlsList = outpatientMapper.listHistoryRecord(listHistoryRecordPojo);
	        
	        if(jzlsList ==null || jzlsList.size() < 1 ){
	        	response.setMessage("未查询到数据！");
	        }else{
				List<HospitalInformation> hospitalInformationList = hospitalInformationMapper.selectList(listHistoryRecordPojo.getOrganizationId());
	        	for(ListHistoryRecordDomain jzls :jzlsList){
	        		for (HospitalInformation yymc:hospitalInformationList){
	        			if (jzls.getOrganizationId().equals(yymc.getOrganizationId())){
							jzls.setOrganizationName(yymc.getOrganizationName());
						}
					}
	        	}
	        	
	        	response.setMessage("处理成功");
				response.setData(jzlsList);
	        }	
	
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(Constant.errorTag);
			response.setMessage("处理失败"+e.getMessage());
		}
		
		
		return response;
	}
	
	/**
	 * 获取门诊患者列表。
	 * @return
	 */
	public BaseResponse<ListPatientDomain> listPatient(ListPatientPojo listPatientPojo){
		
		BaseResponse<ListPatientDomain> response = new BaseResponse<>();
		response.setCode(Constant.successResponse);
		response.setData(null);
		try {
			
			 Long jgid;
	         if(listPatientPojo.getOrganizationId() == null || "".equals(listPatientPojo.getOrganizationId())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("医院识别代码不能为空!");
	        	  
	             return response;
	         }
	           
	        jgid = Long.parseLong(listPatientPojo.getOrganizationId());
			
	        if(listPatientPojo.getDoctorId() == null || "".equals(listPatientPojo.getDoctorId())){
	        	 response.setCode(Constant.errorTag);
	        	 response.setMessage("医生工号不能为空!");
	        	  
	             return response;
	         }

			if(listPatientPojo.getPageNo() == null || "".equals(listPatientPojo.getPageNo())){
				response.setCode(Constant.errorTag);
				response.setMessage("入参页码不能为空!");

				return response;
			}

			if(listPatientPojo.getPageSize() == null || "".equals(listPatientPojo.getPageSize())){
				response.setCode(Constant.errorTag);
				response.setMessage("入参每页条数不能为空!");

				return response;
			}

	        String ygdm = listPatientPojo.getDoctorId();
	        Date dateBegin = null ;
	        Date dateEnd;
	        
	        dateEnd = listPatientPojo.getScheduleDate();
	        if(dateEnd != null){
	        	 //获取有效效期内的就诊记录
		        String ghxq = systemConfigMapper.getValueByConfigName(jgid, "GHXQ"); //挂号效期 默认2天
		        if(ghxq == null){
		        	ghxq = "2";
		        }
		        
		        Integer regValidHours ;
		        regValidHours = (int) Math.round(Double.parseDouble(ghxq) * 24) ;
		        
		        
		        dateBegin = DateUtil.addDateToHour(dateEnd,(-1) * regValidHours);
		        dateEnd = DateUtil.getDateByZeroTime(dateEnd, 1);
	        }
	     
	        List<ListPatientDataDomain> patientList = outpatientMapper.listPatient(jgid,listPatientPojo.getDoctorId(),listPatientPojo.getQuery(),
	        		dateBegin,dateEnd,listPatientPojo.getPageNo(),listPatientPojo.getPageSize());
	        
	        ListPatientDomain listPatient = new ListPatientDomain();
	    
	        Integer wjz = 0;
	        Integer jzz = 0;
	        Integer yjz = 0;
	        for(ListPatientDataDomain patient: patientList){
	        	if("1".equals(patient.getMedicalStatus())){
	        		wjz ++;
	        	}
	        	if("2".equals(patient.getMedicalStatus())){
	        		jzz ++;
	        	}
	        	if("3".equals(patient.getMedicalStatus())){
	        		yjz ++;
	        	}
	        }
	        listPatient.setNotVisit(wjz);
	        listPatient.setVisiting(jzz);
	        listPatient.setVisited(yjz);
	        listPatient.setTotalPatient(patientList.size());
	        listPatient.setPatientList(patientList);
	        
	        response.setMessage("处理成功");
			response.setData(listPatient);
	        
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(Constant.errorTag);
			response.setMessage("处理失败"+e.getMessage());
		}
		
		
		return response;
	}
	
	
}
